/***********************************************************************************************
																						  
This dofile replicates "Figure 1: Effect of banking deregulations on loan volume and houses 
prices", in <<Difference-in-Differences Estimators of Intertemporal Treatment Effects, Clément
 de Chaisemartin, Xavier D'Haultfœuille, (2024)>>.                                            

***********************************************************************************************/

/*******************************************************************************************************************************************************

2) LP regressions in Figure 2 Panel B of <<Favara, Giovanni, and Jean Imbs. 2015. "Credit supply and the price of housing." American Economic Review, 105(3): 958–92>>.

*******************************************************************************************************************************************************/
xtset county year

xtreg Dl_vloans_b L.Dl_vloans_b $D_control  yr* Linter_bra, fe cluster(state_n) 
	forvalues y = 1/8 {
			xtreg F`y'.Dl_vloans_b L.Dl_vloans_b $D_control  yr* Linter_bra, fe cluster(state_n) 
	}


/***********************************************************************************************
                                          STEPS GENERATING FIGURE 1
***********************************************************************************************/

/***********************************************************************************************
 ii) Graph loan volume, estimation 1 : Local projection in Panel 1 (top-left) of Figure 1.
***********************************************************************************************/

matrix estimates1_loan = J(8+3+1,4,0)
matrix colnames estimates1_loan= "Estimate" "LB CI" "UB CI" "x_axis"

xtset county year
xtreg Dl_vloans_b  inter_bra yr*, fe cluster(state_n) 

//save the point estimates, lb and ub

forvalues i=1/3{
	matrix estimates1_loan[`i', 1] = .
	matrix estimates1_loan[`i', 2] = .
	matrix estimates1_loan[`i', 3] = .
	matrix estimates1_loan[`i', 4] = . 
}

matrix estimates1_loan[3, 1] = 0
matrix estimates1_loan[3, 2] = 0
matrix estimates1_loan[3, 3] = 0
matrix estimates1_loan[3, 4] = 0

matrix estimates1_loan[1+3,1] = e(b)[1,1]
matrix estimates1_loan[1+3,2] = e(b)[1,1]-1.96*sqrt(e(V)[1,1])
matrix estimates1_loan[1+3,3] = e(b)[1,1]+1.96*sqrt(e(V)[1,1])
matrix estimates1_loan[1+3,4] = 1

forvalues y = 1/8 {
	xtreg F`y'.Dl_vloans_b inter_bra yr* , fe cluster(state_n) 
				
	//save the point estimates, lb and ub
	local rownames  `rownames' "Effect_`=`y'+1'"
	matrix estimates1_loan[`=`y'+1+3',1] = e(b)[1,1]
	matrix estimates1_loan[`=`y'+1+3',2] = e(b)[1,1]-1.96*sqrt(e(V)[1,1])
	matrix estimates1_loan[`=`y'+1+3',3] = e(b)[1,1]+1.96*sqrt(e(V)[1,1])
	matrix estimates1_loan[`=`y'+1+3',4] = `=`y'+1'
	}

capture drop estimates1_loan*
svmat estimates1_loan

/***********************************************************************************************
 ii) Graph housing prices, estimation 1  : Local projection in Panel 4 (center-left) of Figure 1.
***********************************************************************************************/

matrix estimates1_price = J(8+3+1,4,0)
matrix colnames estimates1_price= "Estimate" "LB CI" "UB CI" "x_axis"

xtset county year

xtreg Dl_hpi inter_bra yr*  [aw=w1], fe cluster(state_n) 

forvalues i=1/3{
matrix estimates1_price[`i', 1] = .
matrix estimates1_price[`i', 2] = .
matrix estimates1_price[`i', 3] =.
matrix estimates1_price[`i', 4] = . //-`i'
}

matrix estimates1_price[3, 1] = 0
matrix estimates1_price[3, 2] = 0
matrix estimates1_price[3, 3] = 0
matrix estimates1_price[3, 4] = 0

matrix estimates1_price[1+3,1] = e(b)[1,1]
matrix estimates1_price[1+3,2] = e(b)[1,1]-1.96*sqrt(e(V)[1,1])
matrix estimates1_price[1+3,3] = e(b)[1,1]+1.96*sqrt(e(V)[1,1])
matrix estimates1_price[1+3,4] = 1
	forvalues y = 1/8 {
			xtreg F`y'.Dl_hpi inter_bra yr*  [aw=w1], fe cluster(state_n) 
			
//save the point estimates, lb and ub
matrix estimates1_price[`=`y'+1+3',1] = e(b)[1,1]
matrix estimates1_price[`=`y'+1+3',2] = e(b)[1,1]-1.96*sqrt(e(V)[1,1])
matrix estimates1_price[`=`y'+1+3',3] = e(b)[1,1]+1.96*sqrt(e(V)[1,1])
matrix estimates1_price[`=`y'+1+3',4] = `=`y'+1'
	}

/***********************************************************************************************
 iii) Graph loan volume, estimation 2 : Non-normalized Effects - in Panel 2 (center-top) of Figure 1.
***********************************************************************************************/

matrix estimates2_loan = J(8+3+1,4,0)
matrix colnames estimates2_loan= "Estimate" "LB CI" "UB CI" "x_axis"

did_multiplegt_dyn Dl_vloans_b county year inter_bra, effects(8) placebo(3) cluster(state_n) graph_off 

forvalues i=1/3{
matrix estimates2_loan[`i', 1] = e(Placebo_`i')
matrix estimates2_loan[`i', 2] = e(Placebo_`i') - 1.96*e(se_placebo_`i')
matrix estimates2_loan[`i', 3] = e(Placebo_`i') + 1.96*e(se_placebo_`i')
matrix estimates2_loan[`i', 4] = -`i'
}
forvalues i=4/11{
matrix estimates2_loan[`i', 1] = e(Effect_`=`i'-3')
matrix estimates2_loan[`i', 2] = e(Effect_`=`i'-3') - 1.96*e(se_effect_`=`i'-3')
matrix estimates2_loan[`i', 3] = e(Effect_`=`i'-3') + 1.96*e(se_effect_`=`i'-3')
matrix estimates2_loan[`i', 4] = `=`i'-3'
}

matrix estimates2_loan[12, 1] = 0
matrix estimates2_loan[12, 2] = 0
matrix estimates2_loan[12, 3] = 0
matrix estimates2_loan[12, 4] = 0

capture drop estimates2_loan*
svmat estimates2_loan

/***********************************************************************************************
 iv) Graph house prices, estimation 2 : Non-normalized Effects - in Panel 6 (bottom-left) of Figure 1.
***********************************************************************************************/

matrix estimates2_price = J(8+3+1,4,0)
matrix colnames estimates2_price= "Estimate" "LB CI" "UB CI" "x_axis"

did_multiplegt_dyn Dl_hpi county year inter_bra, effects(8) placebo(3) cluster(state_n) weight(w1) graph_off 

forvalues i=1/3{
matrix estimates2_price[`i', 1] = e(Placebo_`i')
matrix estimates2_price[`i', 2] = e(Placebo_`i') - 1.96*e(se_placebo_`i')
matrix estimates2_price[`i', 3] = e(Placebo_`i') + 1.96*e(se_placebo_`i')
matrix estimates2_price[`i', 4] = -`i'
}
forvalues i=4/11{
matrix estimates2_price[`i', 1] = e(Effect_`=`i'-3')
matrix estimates2_price[`i', 2] = e(Effect_`=`i'-3') - 1.96*e(se_effect_`=`i'-3')
matrix estimates2_price[`i', 3] = e(Effect_`=`i'-3') + 1.96*e(se_effect_`=`i'-3')
matrix estimates2_price[`i', 4] = `=`i'-3'
}

matrix estimates2_price[12, 1] = 0
matrix estimates2_price[12, 2] = 0
matrix estimates2_price[12, 3] = 0
matrix estimates2_price[12, 4] = 0

capture drop estimates2_price*
svmat estimates2_price


*Normalized DID, on restricted sample
preserve
quietly{
	gen further_change=(inter_bra-Linter_bra!=0) if years_treated>1
	bys county: egen further_change_tot=total(further_change)
	replace further_change_tot=(further_change_tot>0)
	bys state_n: egen further_change_tot_new=max(further_change_tot)
	drop further_change_tot
	rename further_change_tot_new further_change_tot
	
	drop if year_first_change>=2000&year>=year_first_change
	drop if further_change_tot>0
}
/***********************************************************************************************
 v) Graph house prices, estimation 3 : Normalized Effects - in Panel 6 (bottom-left) of Figure 1.
***********************************************************************************************/

/// Graph house prices, estimation 3: add the same switchers option

matrix estimates3_price = J(8+3+1,4,0)
matrix colnames estimates3_price= "Estimate" "LB CI" "UB CI" "x_axis"

did_multiplegt_dyn Dl_hpi county year inter_bra, effects(8) cluster(state_n) weight(w1) normalized same_switchers graph_off effects_equal


forvalues i=1/3{
matrix estimates3_price[`i', 1] = .
matrix estimates3_price[`i', 2] = .
matrix estimates3_price[`i', 3] =.
matrix estimates3_price[`i', 4] =. //-`i'
}

matrix estimates3_price[3, 1] = 0
matrix estimates3_price[3, 2] = 0
matrix estimates3_price[3, 3] = 0
matrix estimates3_price[3, 4] = 0

forvalues i=4/11{
matrix estimates3_price[`i', 1] = e(Effect_`=`i'-3')
matrix estimates3_price[`i', 2] = e(Effect_`=`i'-3') - 1.96*e(se_effect_`=`i'-3')
matrix estimates3_price[`i', 3] = e(Effect_`=`i'-3') + 1.96*e(se_effect_`=`i'-3')
matrix estimates3_price[`i', 4] = `=`i'-3'
}

matrix estimates3_price[12, 1] = .
matrix estimates3_price[12, 2] = .
matrix estimates3_price[12, 3] = .
matrix estimates3_price[12, 4] = .

capture drop estimates3_price*
svmat estimates3_price

/***********************************************************************************************
 vi) Graph loan volume, estimation 3 : Normalized Effects - in Panel 3 (top-left) of Figure 1.
***********************************************************************************************/

matrix estimates3_loan = J(8+3+1,4,0)
matrix colnames estimates3_loan= "Estimate" "LB CI" "UB CI" "x_axis"

did_multiplegt_dyn Dl_vloans_b county year inter_bra, effects(8) cluster(state_n) normalized graph_off effects_equal
forvalues i=1/3{
matrix estimates3_loan[`i', 1] = .
matrix estimates3_loan[`i', 2] = .
matrix estimates3_loan[`i', 3] =.
matrix estimates3_loan[`i', 4] = . //-`i'
}
matrix estimates3_loan[3, 1] = 0
matrix estimates3_loan[3, 2] = 0
matrix estimates3_loan[3, 3] = 0
matrix estimates3_loan[3, 4] = 0

forvalues i=4/11{
matrix estimates3_loan[`i', 1] = e(Effect_`=`i'-3')
matrix estimates3_loan[`i', 2] = e(Effect_`=`i'-3') - 1.96*e(se_effect_`=`i'-3')
matrix estimates3_loan[`i', 3] = e(Effect_`=`i'-3') + 1.96*e(se_effect_`=`i'-3')
matrix estimates3_loan[`i', 4] = `=`i'-3'
}
matrix estimates3_loan[12, 1] = .
matrix estimates3_loan[12, 2] = .
matrix estimates3_loan[12, 3] = .
matrix estimates3_loan[12, 4] = .

capture drop estimates3_loan*
svmat estimates3_loan

restore


/***********************************************************************************************
                                             OUTPUT FIGURE 1
***********************************************************************************************/

/////Clear and svmat the matrices of interest
clear
svmat estimates1_price
svmat estimates2_price
svmat estimates3_price
svmat estimates1_loan
svmat estimates2_loan
svmat estimates3_loan

*Cleaner Graph
net install cleanplots, from("https://tdmize.github.io/data/cleanplots")
set scheme cleanplots

sort estimates1_loan4

twoway (connected estimates1_loan1 estimates1_loan4, ysc(r(-.1 .1)) ylabel(-.1[.05].1) lpattern(solid)) (rcap estimates1_loan3 estimates1_loan2 estimates1_loan4), xlabel(0[1]9) xtitle("Length of exposure to treatment", size(4)) title("Local projection", size(4)) graphregion(color(white)) plotregion(color(white)) legend(off) name(estimates1_loan, replace) yline(0, lcolor(black)) 

sort estimates2_loan4

twoway (connected estimates2_loan1 estimates2_loan4, ylabel(-.4[.2].4) lpattern(solid)) (rcap estimates2_loan3 estimates2_loan2 estimates2_loan4), xlabel(-3[1]8) xtitle("Length of exposure to treatment", size(4) ) title("Non-normalized event-study estimators", size(4)) graphregion(color(white)) plotregion(color(white)) legend(off) name(estimates2_loan, replace)  yline(0, lcolor(black)) 

sort estimates3_loan4

twoway (connected estimates3_loan1 estimates3_loan4, lpattern(solid) ysc(r(-.06 .05)) ylabel(-.06[.03].06)) (rcap estimates3_loan3 estimates3_loan2 estimates3_loan4), xlabel(0[1]8) xtitle("Length of exposure to treatment", size(4)) title("Normalized event-study estimators", size(4)) graphregion(color(white)) plotregion(color(white)) legend(off) name(estimates3_loan, replace)  yline(0, lcolor(black)) 

sort estimates1_price4

twoway (connected estimates1_price1 estimates1_price4, lpattern(solid) ysc(r(-.01 .024)) ylabel(-.01[.01].02)) (rcap estimates1_price3 estimates1_price2 estimates1_price4), xlabel(0[1]9) xtitle("Length of exposure to treatment", size(small)) title("Local projection", size(4)) graphregion(color(white)) plotregion(color(white)) legend(off) name(estimates1_price, replace)  yline(0, lcolor(black)) 

sort estimates2_price4

twoway (connected estimates2_price1 estimates2_price4, lpattern(solid)ylabel(-.05[.05].1)) (rcap estimates2_price3 estimates2_price2 estimates2_price4), xlabel(-3[1]8) xtitle("Length of exposure to treatment", size(4)) title("Non-normalized event-study estimators", size(4)) graphregion(color(white)) plotregion(color(white)) legend(off) name(estimates2_price, replace)  yline(0, lcolor(black)) 

sort estimates3_price4

twoway (connected estimates3_price1 estimates3_price4,  lpattern(solid) ysc(r(-.00307 .006)) ylabel(-.002[.002].006)) (rcap estimates3_price3 estimates3_price2 estimates3_price4), xlabel(0[1]8) xtitle("Length of exposure to treatment", size(4)) title("Normalized event-study estimators", size(4)) graphregion(color(white)) plotregion(color(white)) legend(off) name(estimates3_price, replace)  yline(0, lcolor(black)) 

capture graph drop first_panel second_panel

graph combine estimates1_loan estimates2_loan estimates3_loan, name(first_panel) title("{fontface Slabo 27px:Log of loan growth}") rows(1) 

graph combine estimates1_price estimates2_price estimates3_price,name(second_panel) title("{fontface Slabo 27px:Log of price growth}") rows(1) 

graph combine first_panel second_panel, cols(1)

graph export  "$outputs/Figure 1.pdf", replace
graph save "$outputs/Figure1", replace
